﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class PhieuMuonSachDAO
    {
        #region 1. Them
        public static bool ThemPhieuMuonSach(PhieuMuonSachDTO pms){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPM", pms.MaPM));
                sqlParams.Add(new SqlParameter("@SoThe", pms.SoThe));
                sqlParams.Add(new SqlParameter("@NgayTra", pms.NgayTra));
                sqlParams.Add(new SqlParameter("@NgayMuon", pms.NgayMuon));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertPHIEUMUON", sqlParams);
                if( n == 1){
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
}
        #endregion
        #region 2. Sua
        public static bool SuaPhieuMuonSach(PhieuMuonSachDTO pms)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPM", pms.MaPM));
                sqlParams.Add(new SqlParameter("@SoThe", pms.SoThe));
                sqlParams.Add(new SqlParameter("@NgayTra", pms.NgayTra));
                sqlParams.Add(new SqlParameter("@NgayMuon", pms.NgayMuon));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_UpdatePHIEUMUON", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 3. Xoa
        public static bool XoaPhieuMuonSach(String mapm){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPM", mapm));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeletePHIEUMUON", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
        #region 4. Tim kiem

        private static int _soSachToiDa = 5;
        public static PhieuMuonSachDTO TimPMS(String mapm){
            PhieuMuonSachDTO result = new PhieuMuonSachDTO();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPM", mapm));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectPHIEUMUON", sqlParams);
                DataRow dr = dt.Rows[0];

                result.MaPM = dr["MaPM"].ToString();
                result.SoThe = dr["SoThe"].ToString();
                result.NgayTra = DateTime.Parse(dr["NgayTra"].ToString());
                result.NgayMuon = DateTime.Parse(dr["NgayMuon"].ToString());
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        public static DataTable TimTatCaPhieuMuonSach(){
            DataTable dt= new DataTable();
            try
            {
                dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectPHIEUMUONsAll");
               
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return dt;
        }
        // public static List<PhieuMuonSachDTO> TimPMSByNgayMuon(DateTime ngaymuon)
        // public static List<PhieuMuonSachDTO> TimPMSBySoThe(String sothe)
        public static bool CheckPMSByID(String mapm){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPM", mapm));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectPHIEUMUON", sqlParams);
                if (dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        //Tim sach ma doc gia chua tra
        public static List<CuonSachDTO> TimSachDGChuaTra(String madg)
        {
            List<CuonSachDTO> rs = new List<CuonSachDTO>();
            try
            {
                // create list sqlparameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@SoThe", madg));
                
                // call stored procedure
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("TimSachDocGiaChuaTra", sqlParams);
                foreach (DataRow dr in dt.Rows)
                {
                    CuonSachDTO result = new CuonSachDTO();
                    result.MaSach = dr["MaSach"].ToString();
                    result.MaLanXB = SqlDataAccessHelper.ParseByte(dr["MaLanXB"].ToString());
                    result.SoThuTu = SqlDataAccessHelper.ParseInt(dr["SoThuTu"].ToString());
                    result.MaTuaDe = dr["MaTuaDe"].ToString();
                    result.TinhTrang = dr["TinhTrang"].ToString();
                    result.NgayNhap = DateTime.Parse(dr["NgayNhap"].ToString());
                    result.Lent = dr["Lent"].ToString();
                    rs.Add(result);
                }
               
            }
            catch (System.Exception ex)
            {

            }
            return rs;
        }
        //Check co muon sach qua han khong
        public static bool CheckSachDGChuaTra(String sothe)
        {
            List<CuonSachDTO> SachChuaTra = TimSachDGChuaTra(sothe);
            for (int i = 0; i < SachChuaTra.Count; i++)
            {
                int n = ChiTietPhieuTraSachDAO.TinhSoNgayTreCuaTaiLieu(SachChuaTra[i].MaSach);
                if (n > 0)
                {
                    return true;
                }
            }
            return false;
        }
        //Check so sach muon co >5 khong
        public static bool CheckSoSachToiDa(String sothe)
        {
               
                 List<CuonSachDTO> SachChuaTra = TimSachDGChuaTra(sothe);
            if (SachChuaTra.Count>=_soSachToiDa)
            {
                return true;
            }
            
            return false;
        }

        #endregion
    }
}

